package com.rongxue.interceptor;

import com.rongxue.pojo.ObjectResult;
import com.rongxue.service.utils.P2PMessageUtils;
import com.rongxue.utils.CommonUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by yangweijian on 2018/7/8.
 */
public class Interceptor implements HandlerInterceptor {
    /**
     * 非app访问接口拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String signTime = request.getParameter("signTime");
        String sign = request.getParameter("sign");
        if (null == signTime || "".equals(signTime) || null == sign || "".equals(sign)){
            return false;
        }
        long t = System.currentTimeMillis();
        if(t-Long.parseLong(signTime)>5*1000){
           return false;
        }
        P2PMessageUtils p2PMessageUtils = new P2PMessageUtils();
        String md5 = p2PMessageUtils.getMD5(signTime + "kuayou123");
        if(!md5.equals(sign)){
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
